/*
* blogwizard.cpp - This file is the part of the wp-notifier.(code name "luan luan")
* Copyright (C) 2010 Dean Lee <deanlee3@gmail.com>, http://www.deanlee.cn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* Acknowledgements:
*   <> Many thanks to all of you, who have encouraged me to update my articles
*     and code, and who sent in bug reports and fixes.
* 
*/
#include "blogwizard.h"
#include "ui_account.h"
#include <QProgressBar>
#include <QAbstractButton>
#include <QMessageBox>
#include <QFile>
#include "configdlg.h"
#include "wpxmlrpc.h"
#include "common.h"

AccountPage::AccountPage(const Blog* blog, QWidget* parent) : QWizardPage(parent), m_oldBlog(blog)
{
	ui_.setupUi(this);
	setTitle(tr("Add a blog account"));
	registerField("webaddress*", ui_.le_webaddress);
	registerField("username*", ui_.le_username);
	registerField("password*", ui_.le_password);
}

AccountPage::~AccountPage()
{

}

void AccountPage::initializePage()
{
	if (m_oldBlog) {
		ui_.le_webaddress->setText(m_oldBlog->url());
		ui_.le_username->setText(m_oldBlog->userName());
		ui_.le_password->setText(m_oldBlog->password());
	}

}

bool AccountPage::validatePage()
{
	return true;
}

ProgressPage::ProgressPage(QWidget* parent) : QWizardPage(parent)
{
	setTitle(tr("Setting up your blog account"));
	QVBoxLayout *vb = new QVBoxLayout(this);
	m_label = new QLabel();
	m_label->setText(tr("Detecting blog settings"));
	vb->addWidget(m_label);
	m_progress = new QProgressBar();
	m_progress->setRange(0, 0);
	vb->addWidget(m_progress);
	vb->addStretch(1);
}

ProgressPage::~ProgressPage()
{
}

void ProgressPage::initializePage()
{
	m_label->setText(tr("Detecting blog settings..."));
	wpGetUsersBlogs* cmd = new wpGetUsersBlogs(field("webaddress").toString() + "/xmlrpc.php",
		field("username").toString(), field("password").toString(), this);
	cmd->execute();
	connect(cmd, SIGNAL(finished(bool)), SLOT(xmlRpcFinished(bool)));

}

void ProgressPage::xmlRpcFinished(bool success)
{
	wpCommand* command = static_cast<wpCommand*>(sender());
	BlogWizard* w = (BlogWizard*)wizard();
	if (!success) {
		QMessageBox::critical(this, tr("Error"), command->error());
		w->back();
		return;
	}
	if (command->type() == wpCommand::BlogInfo) {
		wpGetUsersBlogs* cmd = (wpGetUsersBlogs*)command;
		m_label->setText(tr("Fetching comments ..."));
		foreach(Blog blog, cmd->m_blogs) {
			w->m_blogs << blog;
		}
		w->next();
	}
	//else if(command->type() == wpCommand::GetCommentsCount) {
	//	wpGetCommentCount* cmd = (wpGetCommentCount*)command;
	//	foreach(Blog* blog, w->m_blogs) {
	//		if (blog->m_blog_id == cmd->blogId()) {
	//			blog->m_nApprove = cmd->approved;
	//			blog->m_nHold = cmd->awaiting_moderation;
	//			blog->m_nSpam = cmd->spam;
	//		}
	//	}
	//	if (++m_cmdCount == w->m_blogs.count()) {
	//		m_cmdCount = 0;
	//		m_label->setText(tr("Fetching recent comments..."));
	//		foreach(Blog* blog, w->m_blogs) {
	//			connect(blog, SIGNAL(commentsRefreshed()), SLOT(commentsRefreshed()));
	//			blog->refresh();
	//			/*for (int i = 0; i <= CommentData::spam; ++i) {
	//			wpGetComments* getComments = new wpGetComments(*blog, wpGetComments::FetchMore, this);
	//			getComments->execute(0, Comment::statusToString((CommentStatus)i), 0, 1);
	//			connect(getComments, SIGNAL(finished(bool)), SLOT(xmlRpcFinished(bool)));
	//			}*/
	//		}
	//	}


	//}
	//else if (command->type() == wpCommand::GetComments) {
	//	wpGetComments* cmd = (wpGetComments*)command;
	//	/*QFile file(homeDir() + "/" + cmd->m_status + ".xml");
	//	if (file.open(QIODevice::WriteOnly)) {
	//		file.write(command->rawResponse());
	//	}*/
	//	foreach(Blog* blog, w->m_blogs) {
	//		if (blog->m_blog_id == cmd->blogId()) {
	//			foreach(Comment c, cmd->m_comments) {
	//				if (c.dateCreated() > blog->m_lastTimeStamp) {
	//					blog->m_lastTimeStamp = c.dateCreated();
	//				}
	//				if (c.id() > blog->m_nLastId) {
	//					blog->m_nLastId = c.id();
	//				}
	//			}
	//		}
	//	}
	//	
	//	cmd->m_comments.clear();
	//	if (++m_cmdCount == (3 * w->m_blogs.count())) {
	//		m_cmdCount = 0;
	//		w->next();
	//	}
	//}
}


FinishPage::FinishPage(QWidget* parent) : QWizardPage(parent)
{
	setTitle(tr("Your blog has been set up"));
	QVBoxLayout *vb3 = new QVBoxLayout(this);
	m_message = new QLabel();
	vb3->addWidget(m_message);
	vb3->addStretch(1);

}

FinishPage::~FinishPage()
{

}

void FinishPage::initializePage()
{
	BlogWizard* w = (BlogWizard*)wizard();
	if (w->m_blogs.count() == 0) {
		//m_nickname->setText(w->m_blogs.at(0)->m_blog_name); 
	}
	QString message;
	if (w->m_blogs.count() == 1) {
		message = tr("You have added 1 blog");
	}
	else {
		message = tr("You have added %1 blogs").arg(w->m_blogs.count());
	}
	foreach(Blog blog, w->m_blogs) {
		message += "\n" + blog.name();
	}
	m_message->setText(message);
}


BlogWizard::BlogWizard(const Blog* blog, QWidget* parent) : QWizard(parent)
{
	m_oldBlog = blog;
	setModal(true);
	// create pages
	setWindowTitle(tr("Update Account Configuration"));
	AccountPage* page = new AccountPage(blog, this);
	addPage(page);
	ProgressPage* page2 = new ProgressPage(this);
	addPage(page2);
	FinishPage* page3 = new FinishPage(this);
	addPage(page3);
}


BlogWizard::~BlogWizard()
{
	m_blogs.clear();
}


